<template>
  <div>
    <div class="ser_div">
      <el-row>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 员工 </el-col>
            <el-col :span="16">
              <el-select filterable clearable v-model="ser.employee_user_id" placeholder="请选择员工" style="width: 100%">
                <el-option v-for="(item, index) in EmployeeList" :key="index" :label="item.employee_name"
                  :value="item.user_id">
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-col>
        <!-- <el-col>
        <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 订单号 </el-col>
            <el-col :span="16">
              <el-input v-model="ser.order_number" placeholder="请输入订单号"></el-input>
            </el-col>
        </el-row>
      </el-col> -->
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 工资日期范围 </el-col>
            <el-col :span="16">
              <el-date-picker v-model="order_data" @change="order_data_change" value-format="yyyy-MM-dd" type="daterange"
                range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
              </el-date-picker>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="20">
          <el-button @click="search(0)" class="add_btn" icon="el-icon-search">
            搜索</el-button>
          <el-button @click="searchs" icon="el-icon-refresh" class="add_btn">
            重置</el-button>
          <el-button @click="search(2)" class="add_btn">
            导出</el-button>
        </el-col>
      </el-row>
    </div>
    <el-table :data="dataList" v-loading="loading" stripe style="width: 100%" border>
      <el-table-column label="员工姓名">
        <template slot-scope="scope">
          <span v-if="scope.row.employee">
            {{ scope.row.employee.employee_name }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="工种">
        <template slot-scope="scope">
          <span>
            {{ scope.row.job_type ? scope.row.job_type.job_type : '' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="order_count" label="工单完成数"> </el-table-column>
      <el-table-column prop="base_pay" label="员工工资">
        <template slot-scope="scope">
          <span v-if="scope.row.employee">
            {{ scope.row.employee.base_pay }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="total_sales" label="工单总金额"> </el-table-column>
      <el-table-column prop="created_at" label="合计">
        <template slot-scope="scope">
          <span v-if="scope.row.employee && scope.row.total_sales">
            {{
              Number(scope.row.employee.base_pay) +
              Number(scope.row.total_sales)
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="set_product(scope.row.employee.employee_name, scope.row.employee_user_id)" type="text"
            icon="el-icon-edit">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background @prev-click="prev"
        @next-click="next" :current-page.sync="pagination.page" :page-size="pagination.per_page"
        layout="total, prev, pager, next" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="tits + '工资详情'" :visible.sync="isjilu" width="55%" @close="close_dialog">
      <div>
        <!-- <el-row>
        <el-col :span="3"> 
          <el-input v-model="color" placeholder="请输入员工名称"></el-input>
        </el-col>
        <el-col :span="4">
          <el-button @click="clolr_click"  class="add_btn"  icon="el-icon-search"> 搜索</el-button>
        </el-col>
      </el-row> -->
        <div class="btn" style="text-align: right;">
          <el-button @click="search_more()" type="primary" size="mini" class="add_btn">导出</el-button>
        </div>

        <el-table :data="dataList_2" stripe style="width: 100%" border>
          <!-- <el-table-column label="客户">
            <template slot-scope="scope">
              {{ scope.row.order.customer.customer_name }}
            </template>
          </el-table-column> -->
          <el-table-column label="订单号">
            <template slot-scope="scope">
              {{ scope.row.order.order_number }}
            </template>
          </el-table-column>
          <el-table-column prop="order_count" label="产品">
            <template slot-scope="scope">
              {{ scope.row.order_second.product ? scope.row.order_second.product.product_name : '' }}
            </template>
          </el-table-column>
          <!-- <el-table-column label="颜色">
            <template slot-scope="scope">
              {{ scope.row.order.color.color }}
            </template>
          </el-table-column> -->
          <el-table-column label="型号">
            <template slot-scope="scope">
              {{ scope.row.order_second.model_number ? scope.row.order_second.model_number.model_number : '' }}
            </template>
          </el-table-column>
          <el-table-column label="规格">
            <template slot-scope="scope">
              {{ scope.row.order_second.specification ? scope.row.order_second.specification.specification : '' }}
            </template>
          </el-table-column>
          <el-table-column label="金额">
            <template slot-scope="scope">
              {{ scope.row.price }}
            </template>
          </el-table-column>
          <el-table-column label="备注">
            <template slot-scope="scope">
              {{ scope.row.order_second.remark }}
            </template>
          </el-table-column>
          <el-table-column prop="submit_time" label="完成时间" :formatter="formatterTime">
          </el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click="set_product_2(scope.row)" type="text" icon="el-icon-edit">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination @size-change="handleSizeChange_2" @current-change="handleCurrentChange_2" background
            @prev-click="prev_2" @next-click="next_2" :current-page.sync="pagination_2.page"
            :page-size="pagination_2.per_page" layout="total, prev, pager, next" :total="total_2">
          </el-pagination>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isjilu = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="isProduct" width="30%" :close-on-click-modal="false" @close="close_dialog_2">
      <el-form :model="ruleForms" :rules="rules" ref="ruleForms" label-width="100px" class="demo-ruleForm">
        <el-form-item label="金额" prop="color">
          <el-input-number :precision="0" v-model="ruleForms.price" placeholder="请输入金额"></el-input-number>

        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isProduct = false">取 消</el-button>
        <el-button type="primary" @click="submitadd_2('ruleForms')">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getSalaryList,
  createColor,
  getSalaryInfo,
  changeSalary,
  getEmployeeList,
  getSalaryList_exports,
  getSalaryInfo_exports
} from "@/api/product/index";
export default {
  data() {
    return {
      formatterTime(row, column) {
        let data = row[column.property];
        return /\d{4}-\d{1,2}-\d{1,2}/g.exec(data);
      },
      order_data:'',
      loading: true,
      ser: {
        employee_user_id: '',
        employee_name: "",
        start_time: '',
        end_time: ''
      },
      tits: "",
      isjilu: false,
      title: "",
      isProduct: false,
      dataList: [],
      dataList_2: [],
      ruleForm: {
        color: "",
        id: "",
      },
      rules: {
        color: [
          {
            required: true,
            message: "请输入颜色或以英文, 号隔开",
            trigger: "blur",
          },
        ],
      },
      pagination: {
        per_page: 10,
        page: 1,
      },
      total: 0,
      ruleForms: {
        order_child_id: "",
        price: "",
      },
      rules: {
        price: [
          {
            required: true,
            message: "请输入金额",
            trigger: "blur",
          },
        ],
      },
      pagination_2: {
        per_page: 10,
        page: 1,
      },
      total_2: 0,
      box_id: '',
      EmployeeList: []
    };
  },
  mounted() {
    this.getlist();
    this.get_EmployeeList()
    this.gettime()
  },
  methods: {
    gettime(){
      this.getCurrentMonthFirst()
      this.getCurrentMonthLast()
      let order_data=[]

      order_data[0]=this.ser.start_time
      order_data[1]=this.ser.end_time
      console.log(this.order_data=order_data)
    },
    getCurrentMonthFirst() {
      var date = new Date()

      date.setDate(1)

      var month = parseInt(date.getMonth() + 1)

      var day = date.getDate()

      if (month < 10) month = '0' + month

      if (day < 10) day = '0' + day

      this.ser.start_time = date.getFullYear() + '-' + month + '-' + day
      console.log(this.ser.start_time)

    },
    getCurrentMonthLast() {

      var date = new Date()

      var year = date.getFullYear()

      var month = date.getMonth() + 1

      month = month < 10 ? '0' + month : month

      var day = new Date(year, month, 0)

      this.ser.end_time = year + '-' + month + '-' + day.getDate()
      console.log(this.ser.end_time)

    },

    search_more() {
      getSalaryInfo_exports({
        export: 1, employee_user_id: this.box_id, ...this.pagination_2, ...this.more_order, start_time: this.ser.start_time,
        end_time: this.ser.end_time
      }).then(res => {
        //创建一个隐藏的a连接，
        const link = document.createElement("a");
        let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
        link.style.display = "none";
        //设置连接
        link.href = URL.createObjectURL(blob);
        link.download = this.tits + "的工资详情";
        document.body.appendChild(link);
        //模拟点击事件
        link.click();
      })
    },
    get_EmployeeList() {
      getEmployeeList({ per_page: 9999 }).then(res => {
        if (res.code == 200) {
          this.EmployeeList = res.data.data
        }
      })
    },
    order_data_change(val) {
      this.ser.start_time = val[0];
      this.ser.end_time = val[1];
      console.log(this.order_data)
    },
    searchs() {
      this.ser = {
        employee_user_id: '',
        employee_name: "",
        start_time: '',
        end_time: ''
      }
      getSalaryList({ ...this.ser, ...this.pagination }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    search(type) {
      getSalaryList(this.ser).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
          if (type == 2) {
            if (res.data.data.length > 0) {
              this.$message.success("正在导出订单列表");
              this.exports_w();
            }
          }
        }
      });
    },
    exports_w() {
      let data = {
        ...this.ser,
        export: 1,
      };
      getSalaryList_exports(data).then((res) => {
        //创建一个隐藏的a连接，
        const link = document.createElement("a");
        let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
        link.style.display = "none";
        //设置连接
        link.href = URL.createObjectURL(blob);
        link.download = "员工工资列表";
        document.body.appendChild(link);
        //模拟点击事件
        link.click();
      });
    },
    close_dialog() {

    },
    
    getlist() {
      getSalaryList({ ...this.ser, ...this.pagination }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
          this.loading = false;
        }
      });
    },
    set_product(name, id) {
      console.log(id);
      this.isjilu = true
      this.tits = name
      console.log(name)
      this.box_id = id
      this.getlist_2()
    },
    submitadd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          createColor(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.isProduct = false;
              this.$message.success("提交成功");
              this.getlist();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleSizeChange(val) {
      console.log(val);
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagination.page = val;
      this.getlist();
    },
    prev() {
      this.pagination.page = this.pagination.page - 1;
      this.getlist();
    },
    next() {
      console.log("ss");
      this.pagination.page = this.pagination.page + 1;
      this.getlist();
    },
    close_dialog_2() {
      this.ruleForms = {
        order_child_id: "",
        price: "",
      };
    },
    getlist_2() {
      getSalaryInfo({ employee_user_id: this.box_id, ...this.pagination_2, start_time: this.ser.start_time, end_time: this.ser.end_time }).then((res) => {
        if (res.code == 200) {
          this.dataList_2 = res.data.data;
          this.total_2 = res.data.total;
        }
      });
    },
    set_product_2(row) {
      console.log(row)
      this.ruleForms.price = row.price
      this.ruleForms.order_child_id = row.id;
      this.title = "工资修改";
      this.isProduct = true;
    },
    submitadd_2(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          changeSalary(this.ruleForms).then((res) => {
            if (res.code == 200) {
              this.isProduct = false;
              this.$message.success("提交成功");
              this.getlist_2();
              this.getlist();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleSizeChange_2(val) {
      console.log(val);
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange_2(val) {
      console.log(`当前页: ${val}`);
      this.pagination_2.page = val;
      this.getlist_2();
    },
    prev_2() {
      this.pagination_2.page = this.pagination_2.page - 1;
      this.getlist_2();
    },
    next_2() {
      console.log("ss");
      this.pagination_2.page = this.pagination_2.page + 1;
      this.getlist_2();
    },
  },
};
</script>

<style></style>
